

/*
* Copyright @ 2017 com.iflysse.trains
* crm 下午9:08:13
* All right reserved.
*
*/
	
package com.qf.dao.daoImpl;



import java.util.List;

import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.qf.dao.CrmPostDao;
import com.qf.pojo.CrmDepartment;
import com.qf.pojo.CrmPost;
import com.qf.pojo.PageBean;
import com.qf.pojo.PagePost;

/**
* @desc: crm
* @descripe:XXXX
* @author: 周锐
* @createTime: 2017年7月24日 下午9:08:13
* @history:
* @version: v1.0

*/
@Transactional(value="txManager")
public class CrmPostDaoImpl implements CrmPostDao {
    
	private HibernateTemplate htl;
	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmPostDao#addPost(com.qf.pojo.CrmPost)
	*/

	
	/**
	* @param htl the htl to set
	*/
	
	public void setHtl(HibernateTemplate htl) {
	
		this.htl = htl;
	}

	@Override
	public void addPost(CrmPost post) {

		htl.save(post);

	}

	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmPostDao#findbyPage(int, int)
	*/

	@Override
	public PagePost findbyPage(int page, int size) {

		List<CrmPost> posts=htl.getSessionFactory().getCurrentSession().createQuery("from CrmPost",CrmPost.class).setFirstResult((page-1)*size).setMaxResults(size).getResultList();
		int count=htl.getSessionFactory().getCurrentSession().createQuery("from CrmPost",CrmPost.class).getResultList().size();
		int countpage=(count-1)/size+1;
		return new PagePost(page,size,countpage,posts);

	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmPostDao#update(com.qf.pojo.CrmPost)
	*/
		
	@Override
	public void updatePost(CrmPost post) {
		
		htl.update(post);
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmPostDao#findbyId(java.lang.Long)
	*/
		
	@Override
	public CrmPost findbyId(Long postId) {
		
		//TODO
		return htl.get(CrmPost.class, postId);
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmPostDao#findbyAllcrmStaff()
	*/
		
	@Override
	public List<CrmPost> findbyAllPost(Long deptId) {
		
		CrmDepartment crmDepartment = htl.get(CrmDepartment.class, deptId);
		return htl.getSessionFactory().getCurrentSession().createQuery("from CrmPost where crmDepartment=?",CrmPost.class).setParameter(0,crmDepartment).getResultList();
			
	}

}

	